using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using NHibernate.Expression;
using NHibernate.SqlCommand;


namespace FutureOpen.Core.func.db
{
    public class MyTagJob : MyDBEntity
    {
        private Usr usr;
        private IList<Tagjob> mytags;
        public MyTagJob(){}
        public MyTagJob(int uid)
        {
            this.usr = new Usr();
            this.usr.Usr_id = uid;
        }
        public MyTagJob(Usr usr)
        {
            this.usr = usr; 
        }
        private void LoadMyTag()
        {
            using (ISession session = base.factory.OpenSession())
            {
                try
                {
                    IList<Tagjobmap> tags = session.CreateCriteria(typeof(Tagjobmap)).Add(Expression.Eq("Tagjobmap_usr", usr)).List<Tagjobmap>();
                    mytags = new List<Tagjob>();
                    foreach (Tagjobmap map in tags)
                    {
                        mytags.Add(map.Tagjobmap_tag);
                    }
                }
                catch (Exception)
                {
                    mytags = null;
                }
            }
        }
        public void AddTag(string text)
        {
            text = text.ToLower();
            using (ISession session = base.factory.OpenSession())
            {
                Tagjob tag = null;
                IList<Tagjob> tags = session.CreateCriteria(typeof(Tagjob))
                                                   .Add(Expression.Eq("Tagjob_text", text))
                                                   .List<Tagjob>();

                ITransaction transation = session.BeginTransaction();
                if (tags.Count == 0)
                {
                    tag = new Tagjob();
                    tag.Tagjob_text = text;
                    tag.Tagjob_count = 1;

                    session.Save(tag);
                    session.Flush();
                }
                else
                {
                    tag = tags[0];
                    tag.Tagjob_count++;
                    session.Update(tag);
                }

                //insert record        
                Tagjobmap map = new Tagjobmap(usr, tag);
                session.Save(map);
                transation.Commit();
            } 
 
        }
        public void AddTag(Tagjob tag)
        {
            tag.Tagjob_text = tag.Tagjob_text.ToLower();
            using (ISession session = base.factory.OpenSession())
            {
                ITransaction transation = session.BeginTransaction();
                tag.Tagjob_count++;
                session.Update(tag);            

                //insert record
                Tagjobmap map = new Tagjobmap(usr, tag);
                session.Save(map);
                transation.Commit();
                
            } 
        }
        public void DelTagUsr(string tag)
        {
            using (ISession session = base.factory.OpenSession())
            {
                try
                {
                    Tagjob tj = LoadTagByText(tag);
                    tj.Tagjob_count--;
                    session.Update(tj);
                    IList<Tagjobmap> tags = session.CreateCriteria(typeof(Tagjobmap)).Add(Expression.Eq("Tagjobmap_tag", tj)).List<Tagjobmap>();
                    session.Delete(tags[0]);
                    session.Flush();     
                }
                catch(Exception e){}
            }
        }
        public Tagjob GetTagByID(int id)
        {
            using (ISession session = base.factory.OpenSession())
            {
                IList<Tagjob> tags = session.CreateCriteria(typeof(Tagjob))
                                                   .Add(Expression.Eq("Tagjob_id", id))
                                                   .List<Tagjob>();
                if (tags.Count == 0)
                {
                    return null;
                }
                else
                {
                    return tags[0];
                }
            }
        }
        public Tagjob LoadTagByText(string text)
        {
            using (ISession session = base.factory.OpenSession())
            {
                IList<Tagjob> tags = session.CreateCriteria(typeof(Tagjob))
                                                   .Add(Expression.Eq("Tagjob_text", text))
                                                   .List<Tagjob>();
                if (tags.Count == 0)
                {
                    return null;
                }
                else
                {
                    return tags[0];
                }
            }
        }
        public IList<Art> GetArtsByTag(string tagtext, int start, int range)
        {
            using (ISession session = base.factory.OpenSession())
            {
                try
                {
                    
                    IQuery query = session.CreateQuery("from Art where Art_title like ? and DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(Art_time) order by Art_time desc");
                    query.SetString(0, "%" + tagtext + "%");                    
                    query.SetFirstResult(start);
                    query.SetMaxResults(range);
                    IList<Art> artList = query.List<Art>();
                    return artList;
                }
                catch(Exception e)
                {
                    return null;
                }
            }
        }

        public int GetTagCount()
        {
            using (ISession session = base.factory.OpenSession())
            {
                try
                {
                    IQuery iquery = session.CreateQuery("select count(*) from tagJobmap");
                    object obj = iquery.UniqueResult();
                    return Convert.ToInt32(obj);
                }
                catch
                {
                    return 0;
                }
            }
        }

        public int GetTagCount(int usr)
        {
            using (ISession session = base.factory.OpenSession())
            {
                try
                {
                    IQuery iquery = session.CreateQuery("select count(*) from tagJobmap where tagJobmap_usr="+usr);
                    object obj = iquery.UniqueResult();
                    return Convert.ToInt32(obj);
                }
                catch
                {
                    return 0;
                }
            }
        }
        public int GetArtCountByTag(string tagtext)
        {
            using (ISession session = base.factory.OpenSession())
            {
                try
                {
                    IQuery iquery = session.CreateQuery("select count(*) from Art where Art_title like ? and DATE_SUB(CURDATE(), INTERVAL 2 DAY) <= date(Art_time) order by Art_time desc");
                    iquery.SetString(0, "%" + tagtext + "%");
                    object obj = iquery.UniqueResult();
                    return Convert.ToInt32(obj);
                }
                catch
                {
                    return 0;
                }
            }
        }
        public IList<Tagjob> GetUsrTags()
        {
            if (mytags == null)
            {
                LoadMyTag();
            }
            return mytags;
        }

        public IList<Tagjob> GetUsrTags(int usrid, int first, int count)
        {
            using (ISession session = base.factory.OpenSession())
            {
                try
                {
                    Usr u = new Usr();
                    u.Usr_id = usrid;
                    ICriteria criteria = session.CreateCriteria(typeof(Tagjobmap)).Add(Expression.Eq("Tagjobmap_usr", u));
                    criteria.SetFirstResult(first);
                    criteria.SetMaxResults(count);                    
                    IList<Tagjobmap> tags = criteria.List<Tagjobmap>();
                    mytags = new List<Tagjob>();
                    foreach (Tagjobmap map in tags)
                    {
                        mytags.Add(map.Tagjobmap_tag);
                    }

                }
                catch (Exception)
                {
                    mytags = null;
                }
                return mytags;
            }
                   
        }

        public IList<Tagjob> GetTags(int first, int count)
        {
            using (ISession session = base.factory.OpenSession())
            {
                try
                {
                    ICriteria criteria = session.CreateCriteria(typeof(Tagjob));

                    criteria.AddOrder(Order.Desc("Tagjob_count"));
                    criteria.SetFirstResult(first);
                    criteria.SetMaxResults(count);
                    IList<Tagjob> tags = criteria.List<Tagjob>();

                    return tags;

                }
                catch (Exception)
                {
                    return null;
                }                
            }

        }
    }
}
